// ProntoHelperDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ProntoHelper.h"
#include "ProntoHelperDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProntoHelperDlg dialog

CProntoHelperDlg::CProntoHelperDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CProntoHelperDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CProntoHelperDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProntoHelperDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProntoHelperDlg)
	DDX_Control(pDX, IDC_CHECK1, m_ckTop);
	DDX_Control(pDX, IDC_COMBO2, m_coHotkey);
	DDX_Control(pDX, IDC_COMBO1, m_coServer);
	DDX_Control(pDX, IDC_PRONTOID, m_edProntoID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CProntoHelperDlg, CDialog)
	//{{AFX_MSG_MAP(CProntoHelperDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnStart)
	ON_BN_CLICKED(IDC_BUTTON2, OnStop)
	ON_BN_CLICKED(IDC_BUTTON3, OnGo)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_CHECK1, OnTop)
	ON_WM_SYSCOMMAND()
	ON_BN_CLICKED(IDC_BUTTON4, OnReport)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProntoHelperDlg message handlers

BOOL CProntoHelperDlg::OnInitDialog()
{
	CDialog::OnInitDialog();


	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
    // register Hot key
    m_coHotkey.SetCurSel(0);

	if(RegisterHotKey(this->GetSafeHwnd(), ID_HOTKEY, MOD_WIN, VK_F12) == FALSE)
	{
		AfxMessageBox("regist hotkey failed!");
	}

    // select Pronto Server
    m_coServer.SetCurSel(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}


// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CProntoHelperDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CProntoHelperDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CProntoHelperDlg::OnStart() 
{
    m_mouseover.Init(GetSafeHwnd());
    m_mouseover.start();
}

void CProntoHelperDlg::OnStop() 
{
    m_mouseover.stop();
	
}

void CProntoHelperDlg::OnGo() 
{
	// http://eslpe003.emea.nsn-net.net/nokia/pronto/pronto.nsf/PRID/NA04813398?OpenDocument
    CString szProntoID;
    m_edProntoID.GetWindowText(szProntoID);
    char szPronto[256]= {0};
    sprintf(szPronto,"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE http://eslpe002.emea.nsn-net.net/nokia/pronto/pronto.nsf/PRID/%s?OpenDocument",szProntoID);
    WinExec(szPronto,SW_SHOW);   

    return;


    BOOL bWorked;
    STARTUPINFO suInfo;
    PROCESS_INFORMATION procInfo;
    CString m_Process = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
    memset (&suInfo, 0, sizeof(suInfo));
    suInfo.cb = sizeof(suInfo);
    
    bWorked = ::CreateProcess(m_Process,
        szPronto,      // can also be NULL
        NULL,
        NULL,
        FALSE,
        NORMAL_PRIORITY_CLASS,
        NULL,
        NULL,
        &suInfo,
        &procInfo);
    
        /*
        procInfo has these members
        HANDLE hProcess;   // process handle
        HANDLE hThread;    // primary thread handle
        DWORD dwProcessId; // process PID
        DWORD dwThreadId;  // thread ID
    */
    
    if (procInfo.dwThreadId = NULL)
    {
        MessageBox("nope");
    }
    
    //::CloseHandle(procInfo.dwThreadId);
    

}

LRESULT CProntoHelperDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{

    
    switch (message) {
		case WM_MY_SHOW_TRANSLATION:
            {
			    ExtractProntoID();
            }
			break;
        case WM_HOTKEY:
            {
                WORD wKey = HIWORD(lParam);
		        switch(wKey)
		        {
		        case VK_F12:
                    OnGo();
			        break;
		        }
            }
            break;
		default:
			/*nothing*/;
	}	
	return CDialog::WindowProc(message, wParam, lParam);
}

void CProntoHelperDlg::ExtractProntoID()
{
    CString strCurWord(GlobalData->CurMod.MatchedWord);
    SetWindowText(strCurWord);
    CString strID = m_grabber.ParseProntoID(strCurWord);
    
    // do not change current pronto ID if no new pronto id detected
    if(!strID.IsEmpty())
        m_edProntoID.SetWindowText(strID);

}

void CProntoHelperDlg::OnClose() 
{
	UnregisterHotKey(this->GetSafeHwnd(),ID_HOTKEY);

	
	CDialog::OnClose();
}

void CProntoHelperDlg::OnTop() 
{
    if(m_ckTop.GetCheck())
    {
        // set to top
        SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE); 
    }
    else
    {
        // untop
        SetWindowPos(&wndNoTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE); 
    }
}

void CProntoHelperDlg::OnReport() 
{
    CString szProntoID;
    m_edProntoID.GetWindowText(szProntoID);
    char szPronto[256]= {0};
    sprintf(szPronto,"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE http://eslpe002.emea.nsn-net.net/nokia/pronto/pronto.nsf/PRID/%s?OpenDocument",szProntoID);
    WinExec(szPronto,SW_SHOW);
}
